<?php

namespace App\Http\Controllers\Admin;

use App\Http\Requests\StoreRechargeway;
use App\Http\Requests\UpdateRechargeway;
use App\Models\Rechargeway;
use App\Models\Store;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class RechargewayController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function __construct()
    {
        view()->share([
            '_customers' => 'am-in',
            '_plan' => 'am-active',

        ]);

    }


//企业开通方案首页
    public function index()
    {
//        方案关联门店
       $plans=Rechargeway::with('store')->orderBy('sort')->get();


       return view('admin.rechargeway.index',compact('plans'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    //新增页面
    public function create()
    {
        //所有门店
        $cates=Store::all();
        return view('admin.rechargeway.create',compact('cates'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
//    执行更新
    public function store(StoreRechargeway $request)
    {

           Rechargeway::create($request->all());
           return redirect('/admin/rechargeway')->with('success','新增成功');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */


    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    //更新页面
    public function edit($id)
    {
        //所有门店
        $cates=Store::all();
        //当前方案
        $plan=Rechargeway::find($id);
        return view('admin.rechargeway.edit',compact('plan','cates'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
//    执行更新
    public function update(UpdateRechargeway $request, $id)
    {
        //当前方案
        $plan=Rechargeway::find($id);
        $plan->update($request->all());
        return redirect('admin/rechargeway')->with('success','修改成功');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */

        //AJAX排序
    public function sort(Request $request){
        //当前方案
        $plan = Rechargeway::find($request->id);
        //更新字段
        $plan->sort = $request->sort;
        $plan->save();
    }


    public function destroy($id)
    {
       Rechargeway::destroy($id);
       return back()->with('success','删除成功');

    }



}
